import 'package:aliplayer_widget/aliplayer_widget_lib.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:hktk_app/dao/course_dao.dart';
import 'package:hktk_app/dao/industry_dao.dart';

class FreeVideo extends StatefulWidget {

  final String aliyunVideoId;

  final String videoTitle;

  const FreeVideo(
      {super.key,
      required this.aliyunVideoId,
      required this.videoTitle});

  @override
  State<StatefulWidget> createState() => _FreeVideoState();
}

class _FreeVideoState extends State<FreeVideo> with WidgetsBindingObserver {
  late AliPlayerWidgetController _controller;
  bool _isLoading = true;

  @override
  void initState() {
    //   initState
    super.initState();
    // 添加应用生命周期观察者
    WidgetsBinding.instance.addObserver(this);

    // 设置屏幕保持常亮
    // SystemChrome.setEnabledSystemUIMode(SystemUiMode.immersiveSticky);
    // SystemChrome.setPreferredOrientations([
    //   DeviceOrientation.portraitUp,
    //   DeviceOrientation.landscapeLeft,
    //   DeviceOrientation.landscapeRight,
    // ]);

    _controller = AliPlayerWidgetController(context);
    //   获取播放凭证
    CourseDao.aliyunAuthFree(
            videoId: widget.aliyunVideoId)
        .then((res) {
      _isLoading = false;
      final data = AliPlayerWidgetData(
        videoSource: VideoSourceFactory.createVidAuthSource(
          vid: widget.aliyunVideoId,
          playAuth: res,
        ),
        videoTitle: widget.videoTitle,
      );
      _controller.configure(data); // 设置自动播放
      if(mounted){
        setState(() {

        });
      }
    });
  }
  // @override
  // void didChangeAppLifecycleState(AppLifecycleState state) {
  //   super.didChangeAppLifecycleState(state);
  //
  //   // 根据应用状态控制屏幕常亮
  //   switch (state) {
  //     case AppLifecycleState.resumed:
  //     // 应用恢复时保持屏幕常亮
  //       SystemChrome.setEnabledSystemUIMode(SystemUiMode.immersiveSticky);
  //       break;
  //     case AppLifecycleState.paused:
  //     // 应用暂停时恢复默认设置
  //       SystemChrome.setEnabledSystemUIMode(SystemUiMode.edgeToEdge);
  //       break;
  //     default:
  //       break;
  //   }
  // }
  @override
  void dispose() {
    //   dispose
    super.dispose();
    _controller.destroy();

    // 恢复默认设置
    // SystemChrome.setEnabledSystemUIMode(SystemUiMode.edgeToEdge);
    // SystemChrome.setPreferredOrientations([
    //   DeviceOrientation.portraitUp,
    // ]);

    WidgetsBinding.instance.removeObserver(this);
  }

  @override
  Widget build(BuildContext context) {
    // 
    return Scaffold(
      body: SafeArea(child: OrientationBuilder(
          builder: (BuildContext context, Orientation orientation) {
        if (orientation == Orientation.landscape) {
          return _isLoading
              ? Center(child: CircularProgressIndicator())
              : Column(
                  children: [
                    Expanded(
                        child: AliPlayerWidget(_controller,
                            examId: IndustryDao.getExamKey().examId,
                            overlays: [
                          Positioned(
                            top: 5,
                            right: 5,
                            // cdn/huikao/png/202502/20250215112138605@shuiyin.png
                            child: Image.asset(
                              "images/login/20250215112138605@shuiyin.png",
                              width: 60,
                            ),
                          )
                        ]))
                  ],
                );
        } else {
          return _isLoading
              ? Center(child: CircularProgressIndicator())
              : AliPlayerWidget(_controller,
                  examId: IndustryDao.getExamKey().examId,
                  overlays: [
                      Positioned(
                        top: 5,
                        right: 5,
                        // cdn/huikao/png/202502/20250215112138605@shuiyin.png
                        child: Image.asset(
                          "images/login/20250215112138605@shuiyin.png",
                          width: 60,
                        ),
                      )
                    ]);
        }
      })),
    );
  }
}
